package com.dating.entities;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.location.Location;
import android.os.Handler;
import android.util.Log;

import com.dating.activity.Dating;
import com.dating.libs.client.parttern.asa.ADbObject;
import com.dating.libs.client.parttern.doit.OnAuthListener;
import com.dating.libs.client.parttern.doit.OnFacebookListener;
import com.dating.libs.web.services.Credential;
import com.dating.libs.web.services.datingservice.UserProperties;
import com.dating.libs.web.services.registration.RegistrationCredential;

public class Me extends ADbObject implements Serializable{
	private static final long serialVersionUID = 542052680078190271L;
	
	// Tag
	public final String Tag = "Me";
	public String grapPath="me";
	
	// inner fields
	public String UID = "";
	public String AUTH = "";
	public boolean _status=false;
	public Bitmap _bmp=null;
	
	MeDbManager meMng=null;
	public Credential credential=null;
	public RegistrationCredential registCredential = null;
	public ArrayList<UserProperties> mMeProperties=new ArrayList<UserProperties>();
	OnFacebookListener fbListener=null;
	OnAuthListener lgListener=null;
	OnAuthListener rgListener=null;
	
	// fields
	public String _email = "email";
	public String _phone = "phone";
	public String _gender = "gender";
	public String _address = "address";
	public String _birthday = "birthday";
	public String _fullname = "fullname";
	public String _latitude = "latitude";
	public String _nickname = "nickname";
	public String _category = "category";
	public String _longitude = "longitude";
	public String _link_image = "link_image";
	public String _id_username = "id_username";
	public String _type_account = "type_account";
	public String _current_address = "curent_address";
	
	// fields for Facebook
	public String likes="";
	public String relationship_status="";
	
	// CONSTRUCTOR

	/**
	 * @param:
	 *  _id_username,_fullname,_gender,_birthday,_email,_link_image,_type_account
	 *  ,_address,_latitude,_longitude,_current_address,_phone,_nickname,
	 *  _category
	 * */
	public Me(int index, String... values) {
		super(index, values);
		
		setValue(values);
	}
	// SET-GET
	public void setValue(String... values){
		super.values=values;
		
		this._id_username = values[0];
		this._fullname = values[1];
		this._gender = values[2];
		this._birthday = values[3];
		this._email = values[4];
		this._link_image = values[5];
		this._type_account = values[6];
		this._address = values[7];
		this._latitude = values[8];
		this._longitude = values[9];
		this._current_address = values[10];
		this._phone = values[11];
		this._nickname = values[12];
		this._category = values[13];
	}
	/**
	 * get-set Credential to web-service
	 * */
	public void setCredential(Credential _credential) {
		if (_credential != null)
			this.credential = _credential;
		else
			Log.v(Tag, "_credential null");
	}
	/**
	 * get-set registCredential to web-service
	 * */
	public void setRegistCredential(RegistrationCredential _credential){
		if(_credential !=null){
			this.registCredential=_credential;
		}else
			Log.v(Tag, "_registCredential null");
	}
	/**
	 * Facebook Listener for Login
	 * */
	public void setOnFacebookListener(OnFacebookListener _listener){
		this.fbListener=_listener;
	}
	/**
	 * System Listener for Login
	 * */
	public void setOnLoginListener(OnAuthListener _listener){
		this.lgListener=_listener;
	}
	/**
	 * Register Listener
	 * */
	public void setOnRegisterListener(OnAuthListener _listener){
		this.rgListener=_listener;
	}
	/**
	 * Only has an Object of Me!
	 * */
	public static Me getInstance(Me _me){
		if(_me!=null)
			return _me;
		String[] column={"","","","","","","","","","","","","",""};
		_me=new Me(0, column);
		if(_me.credential==null){
			_me.credential=new Credential();
		}
		if(_me.registCredential==null){
			_me.registCredential=new RegistrationCredential();
		}
		if(_me.mMeProperties==null)
			_me.mMeProperties=new ArrayList<UserProperties>();
		return _me;
	}
	// ME METHOD
	/**
	 * Setup Listener for Facebook Login
	 * */
	public void doFBLogin(final Me me,final Activity activity,final Handler handler){
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				if(fbListener!=null)
					fbListener.startLoginFB(Dating.ME, activity, handler);
			}
		}).start();
		
	}
	/**
	 * Setup Listener for System Login
	 * */
	public void doSYSLogin(final String username,final String password){
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				if(!username.equals("") && !password.equals(""))
					lgListener.onStart(username, password);
			}
		}).start();
	}
	/**
	 * Setup Listener for Register
	 * */
	public void doSYSRegist(final String email,final String password){
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				if(rgListener!=null)
					rgListener.onStart(email, password);
			}
		}).start();
		
	}
	// SUPER METHOD
	@Override
	public String toString() {
		return "Me [Tag=" + Tag + ", credential=" + credential + ", _email="
				+ _email + ", _phone=" + _phone + ", _gender=" + _gender
				+ ", _address=" + _address + ", _birthday=" + _birthday
				+ ", _fullname=" + _fullname + ", _latitude=" + _latitude
				+ ", _nickname=" + _nickname + ", _category=" + _category
				+ ", _longitude=" + _longitude + ", _link_image=" + _link_image
				+ ", _id_username=" + _id_username + ", _type_account="
				+ _type_account + ", _current_address=" + _current_address
				+ ", _id=" + _id + ", values=" + Arrays.toString(values) + "]";
	}
	public void refreshValues(boolean force){
		if(values.equals("") && !force){
			String[] _values={
					_id_username,_fullname,_gender,_birthday,_email,_link_image,_type_account,
					_address,_latitude,_longitude,_current_address,_phone,_nickname,_category};
			super.values=_values;
		}
		else if(force){
			String[] _values={
					_id_username,_fullname,_gender,_birthday,_email,_link_image,_type_account,
					_address,_latitude,_longitude,_current_address,_phone,_nickname,_category};
			super.values=_values;
		}
	}
	// DATABASE
	public void saveToDb(Context context){
		refreshValues(false);
		meMng=new MeDbManager(context, MeDbManager.TABLE, MeDbManager.COLUMNS);
		meMng.updateTo(MeDbManager.TABLE, 0, values);
	}
	public Me restoreFromDb(Context context){
		meMng=new MeDbManager(context, MeDbManager.TABLE, MeDbManager.COLUMNS);
		return meMng.select(0);
	}
	// SERIALIZABLE
	private void writeObject(java.io.ObjectOutputStream out) throws IOException{
		FileOutputStream me=new FileOutputStream("me.sv");
		out=new ObjectOutputStream(me);
		out.writeObject(this);
		out.close();
		me.close();
	}
	private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException{
		FileInputStream me=new FileInputStream("me.sv");
		in=new ObjectInputStream(me);
		Me _me=(Me)in.readObject();
		if(_me!=null){
			Dating.ME=_me;
		}
		in.close();
		me.close();
	}
	public boolean isOnline(){
		return _status;
	}
	public void setOnline(boolean isOnline){
		_status=isOnline;
	}
	public Location getLocation(){
		Location _location=new Location(this._fullname);
		_location.setLatitude(Double.parseDouble(this._latitude));
		_location.setLongitude((Double.parseDouble(this._longitude)));
		return _location;
	}
	public double getDistance(Location src,Location dst){
		return src.distanceTo(dst);
	}
}
